<?php

/**
 * This is the model class for table "portfolio".
 *
 * The followings are the available columns in table 'portfolio':
 * @property integer $id
 * @property string $date
 * @property integer $companyID
 * @property integer $portfolionameID
 * @property integer $quantity
 * @property integer $price
 * @property integer $amount
 * @property integer $quantity_m
 * @property integer $price_m
 * @property integer $amount_m
 * @property integer $type
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property Portfolioname $portfolioname
 * @property Company $company
 */
class Portfolio extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Portfolio the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'portfolio';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('companyID, portfolionameID', 'required'),
            array('companyID, portfolionameID, quantity, amount, quantity_m, amount_m, type, status', 'numerical', 'integerOnly' => true),
            array('price, price_m', 'numerical'),
            array('date', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, date, companyID, portfolionameID, quantity, price, amount, quantity_m, price_m, amount_m, type, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'portfolioname' => array(self::BELONGS_TO, 'Portfolioname', 'portfolionameID'),
            'company' => array(self::BELONGS_TO, 'Company', 'companyID'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'date' => 'Date',
            'companyID' => 'Company',
            'portfolionameID' => 'Portfolio Name',
            'quantity' => 'Quantity -',
            'price' => 'Price -',
            'amount' => 'Amount -',
            'quantity_m' => 'Quantity',
            'price_m' => 'Price',
            'amount_m' => 'Amount',
            'type' => 'Type',
            'status' => 'Status',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;
        $backup = $this->date;
        
        if (preg_match('/[0-3]\d\/[01]\d\/\d{4}/', $this->date)) {
            $this->date = Stock::uk_date_to_mysql_date($this->date);
        }
        $criteria->order = "date desc";
        $criteria->compare('id', $this->id);
        $criteria->compare('date', $this->date, true);
        $criteria->compare('companyID', $this->companyID);
        $criteria->compare('portfolionameID', $this->portfolionameID);
        $criteria->compare('quantity', $this->quantity);
        $criteria->compare('price', $this->price);
        $criteria->compare('amount', $this->amount);
        $criteria->compare('quantity_m', $this->quantity_m);
        $criteria->compare('price_m', $this->price_m);
        $criteria->compare('amount_m', $this->amount_m);
        $criteria->compare('type', $this->type);
        $criteria->compare('status', $this->status);

        $dataProvider = new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
        $this->date = $backup;
        return $dataProvider;
    }

    public function searchPort($portid) {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('date', $this->date, true);
        $criteria->compare('companyID', $this->companyID);
        $criteria->compare('portfolionameID', $this->portfolionameID);
        $criteria->compare('quantity', $this->quantity);
        $criteria->compare('price', $this->price);
        $criteria->compare('amount', $this->amount);
        $criteria->compare('quantity_m', $this->quantity_m);
        $criteria->compare('price_m', $this->price_m);
        $criteria->compare('amount_m', $this->amount_m);
        $criteria->compare('type', $this->type);
        $criteria->compare('status', $this->status);

        $criteria->addCondition("portfolionameID=" . $portid);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function getCompany() {
        $dealList = Company::model()->findAll();

        $arrDeal = array();
        foreach ($dealList as $deal) {
            $arrDeal[$deal->id] = $deal->ticket;
        }
        return $arrDeal;
    }

}